<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CANtegrity API (kvDiag)</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_user_guide_kvdiag.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">CANtegrity API (kvDiag) </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#section_uset_guide_kvdiag_setup">Setting up an analyzer</a></li>
<li class="level1"><a href="#section_user_guide_kvdiag_samples">Samples</a></li>
</ul>
</div>
<div class="textblock"><p>The CANtegrity API can be used to obtain detailed information about the traffic on a CAN bus. To use the CANtegrity API, a device that supports it is needed, such as the Kvaser USBcan Pro 2xHS v2.</p>
<p>If a device supports CANtegrity, it will have the <a class="el" href="canlib_8h.html#a8cc8a1a1960813e55b753943274d1006">canCHANNEL_CAP_CANTEGRITY</a> flag set in its channel capabilities. To check whether the flag is set or not, use the <a class="el" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a> API call with the <a class="el" href="canlib_8h.html#a7eeb70e7c666f629ca3762f4692469ae">canCHANNELDATA_CHANNEL_CAP</a> item as follows. </p><div class="fragment"><div class="line">uint32_t capabilities;</div><div class="line"><a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel, <a class="code" href="canlib_8h.html#a7eeb70e7c666f629ca3762f4692469ae">canCHANNELDATA_CHANNEL_CAP</a>, &amp;capabilities, <span class="keyword">sizeof</span>(capabilities));</div><div class="line"><span class="keywordflow">if</span> (capabilities &amp; canCHANNEL_CAP_CANTEGRITY) {</div><div class="line">  <span class="comment">// Device has CANtegrity support.</span></div><div class="line">}</div></div><!-- fragment --><h1><a class="anchor" id="section_uset_guide_kvdiag_setup"></a>
Setting up an analyzer</h1>
<p>To use the API, both the "canlib.h" and "kvDiag.h" header files need to be included in the program.</p>
<p>First, a CAN channel must be opened on the CANtegrity device to obtain a <a class="el" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a>. </p><div class="fragment"><div class="line"><a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line">hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(channel, <a class="code" href="canlib_8h.html#afacd9e186d273d0dc24956ea0d433822">canOPEN_EXCLUSIVE</a>);</div></div><!-- fragment --><p>Then, attach an analyzer to the open channel. </p><div class="fragment"><div class="line"><a class="code" href="group__kvdiag__diagnostics.html#ga76d09ccb89cf756d954144582f730330">kvDiagAttachAnalyzer</a>(hnd, <a class="code" href="kv_diag_8h.html#a29188edd320816f6f3b4e98e6b331a66">DIAG_ANALYZER_TYPE_DEFAULT</a>);</div></div><!-- fragment --><p>Decide which analyzer program to run. Currently there are two analyzer programs, <a class="el" href="kv_diag_8h.html#ad24c2e14068b790b7b105c6423e42400">DIAG_PROGRAM_TYPE_NORMAL</a> and <a class="el" href="kv_diag_8h.html#a64bb36d4e56a896eb2af5dc09cce1542">DIAG_PROGRAM_TYPE_AUTOBAUD</a>. The former samples CAN frames from the bus and gives detailed information about the timing characteristics, while the latter can be used to find out the bitrate of a running CAN bus.</p>
<p>Choosing an analyzer program is done through the <a class="el" href="group__kvdiag__diagnostics.html#gad615ea3768542e4d48f10d0dca4ef46d">kvDiagSetProgram</a> API call, which requires a JSON configuration as an argument. For <a class="el" href="kv_diag_8h.html#a64bb36d4e56a896eb2af5dc09cce1542">DIAG_PROGRAM_TYPE_AUTOBAUD</a>, no configuration is needed so pass an empty JSON string, "{}". <a class="el" href="kv_diag_8h.html#ad24c2e14068b790b7b105c6423e42400">DIAG_PROGRAM_TYPE_NORMAL</a> needs bus parameters in the configuration, the details of which can be found in the <a class="el" href="group__kvdiag__diagnostics.html#gad615ea3768542e4d48f10d0dca4ef46d">kvDiagSetProgram</a> reference.</p>
<div class="fragment"><div class="line"><a class="code" href="group__kvdiag__diagnostics.html#gad615ea3768542e4d48f10d0dca4ef46d">kvDiagSetProgram</a>(hnd, <a class="code" href="kv_diag_8h.html#ad24c2e14068b790b7b105c6423e42400">DIAG_PROGRAM_TYPE_NORMAL</a>, <span class="stringliteral">&quot;{\&quot;tseg1\&quot; : 59, \&quot;tseg2\&quot; : 20, \&quot;sjw\&quot; : 16, \&quot;brp\&quot; : 2}&quot;</span>);</div></div><!-- fragment --><p>Now, the analyzer program can be started by calling <a class="el" href="group__kvdiag__diagnostics.html#ga9cb62a80b9ffe4ea80b22acfb49bd3a4">kvDiagStart</a>. </p><div class="fragment"><div class="line"><a class="code" href="group__kvdiag__diagnostics.html#ga9cb62a80b9ffe4ea80b22acfb49bd3a4">kvDiagStart</a>(hnd);</div></div><!-- fragment --><p>There are different functions available for interacting with a running analyzer program. <a class="el" href="group__kvdiag__diagnostics.html#ga43a268ee89c38c3ac496d20f35a48105">kvDiagReadSample</a> and <a class="el" href="group__kvdiag__diagnostics.html#gaab9b2bc0856a7200d1f76533092eb023">kvDiagReadSampleWait</a> are used with <a class="el" href="kv_diag_8h.html#ad24c2e14068b790b7b105c6423e42400">DIAG_PROGRAM_TYPE_NORMAL</a> , <a class="el" href="group__kvdiag__diagnostics.html#gabc77cf695c7fb518263d32539a3aec9e">kvDiagCalculateBitrate</a> and <a class="el" href="group__kvdiag__diagnostics.html#ga65bbdc1e8c8bf4affc8ba2041d72ff1a">kvDiagResetBitrateCalculation</a> are used with <a class="el" href="kv_diag_8h.html#a64bb36d4e56a896eb2af5dc09cce1542">DIAG_PROGRAM_TYPE_AUTOBAUD</a>, and <a class="el" href="group__kvdiag__diagnostics.html#ga243548139dcbb64ff273648dd0c370dd">kvDiagCalculateClockOffset</a> and <a class="el" href="group__kvdiag__diagnostics.html#ga1708e589eb731c11c82a621b2a070d1d">kvDiagResetClockOffsetCalculation</a> can be used with both analyzer programs. For details on their usage, see the API reference.</p>
<p>When finished, call <a class="el" href="group__kvdiag__diagnostics.html#ga685c95a3e168601a28e680d9c820dd8a">kvDiagStop</a>, <a class="el" href="group__kvdiag__diagnostics.html#ga61263295b3feaad95c257eef05a9579b">kvDiagDetachAnalyzer</a> and <a class="el" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a> in that order to stop the CANtegrity device.</p>
<div class="fragment"><div class="line"><a class="code" href="group__kvdiag__diagnostics.html#ga685c95a3e168601a28e680d9c820dd8a">kvDiagStop</a>(hnd);</div><div class="line"><a class="code" href="group__kvdiag__diagnostics.html#ga61263295b3feaad95c257eef05a9579b">kvDiagDetachAnalyzer</a>(hnd);</div><div class="line"><a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div></div><!-- fragment --><p>Below, two complete sample programs are provided showcasing how the CANtegrity API is used.</p>
<h1><a class="anchor" id="section_user_guide_kvdiag_samples"></a>
Samples</h1>
<p><br />
<b>Example</b>: Continuously read samples from the CAN bus using <a class="el" href="kv_diag_8h.html#ad24c2e14068b790b7b105c6423e42400">DIAG_PROGRAM_TYPE_NORMAL</a> and print some information about the samples.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;stdio.h&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;signal.h&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="canlib_8h.html">canlib.h</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="kv_diag_8h.html">kvDiag.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> status); <span class="comment">/* Checks for errors */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> intHandler(<span class="keywordtype">int</span> unused); <span class="comment">/* Handle Ctrl-C */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> printSample(<a class="code" href="structkv_diag_sample.html">kvDiagSample</a> *sample); <span class="comment">/* Print sampled frame */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> usage(); <span class="comment">/* Print usage */</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> interrupt = 0;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *CONF_500KBIT = <span class="stringliteral">&quot;{&quot;</span></div><div class="line">                                    <span class="stringliteral">&quot;\&quot;tseg1\&quot; : 59,&quot;</span></div><div class="line">                                    <span class="stringliteral">&quot;\&quot;tseg2\&quot; : 20,&quot;</span></div><div class="line">                                    <span class="stringliteral">&quot;\&quot;sjw\&quot; : 16,&quot;</span></div><div class="line">                                    <span class="stringliteral">&quot;\&quot;brp\&quot; : 2&quot;</span></div><div class="line">                                  <span class="stringliteral">&quot;}&quot;</span>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">  <span class="keywordtype">int</span> channel = -1;</div><div class="line"></div><div class="line">  <span class="comment">/* Parse command-line parameters. */</span></div><div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=1; i&lt;argc; i++) {</div><div class="line">    <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-ch=%d&quot;</span>, &amp;channel) == 1);</div><div class="line">    <span class="keywordflow">else</span> {</div><div class="line">      usage();</div><div class="line">      exit(1);</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (channel == -1) {</div><div class="line">    usage();</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line"></div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line">  <span class="comment">/* We open the channel with the EXCLUSIVE flag to not let any other</span></div><div class="line"><span class="comment">   * application interfere with the CANtegrity channel */</span></div><div class="line">  hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(channel, <a class="code" href="canlib_8h.html#afacd9e186d273d0dc24956ea0d433822">canOPEN_EXCLUSIVE</a>);</div><div class="line"></div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">  <span class="comment">/* Attach an analyzer to an open CAN channel. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga76d09ccb89cf756d954144582f730330">kvDiagAttachAnalyzer</a>(hnd, <a class="code" href="kv_diag_8h.html#a29188edd320816f6f3b4e98e6b331a66">DIAG_ANALYZER_TYPE_DEFAULT</a>);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagAttachAnalyzer&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* We want to run DIAG_PROGRAM_TYPE_NORMAL. It needs a JSON configuration</span></div><div class="line"><span class="comment">   * specifying bus parameters. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#gad615ea3768542e4d48f10d0dca4ef46d">kvDiagSetProgram</a>(hnd, <a class="code" href="kv_diag_8h.html#ad24c2e14068b790b7b105c6423e42400">DIAG_PROGRAM_TYPE_NORMAL</a>, CONF_500KBIT);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagSetProgram&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Start the analyzer. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga9cb62a80b9ffe4ea80b22acfb49bd3a4">kvDiagStart</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagStart&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Continuously read samples and print until Ctrl-C is pressed. */</span></div><div class="line">  signal(SIGINT, intHandler);</div><div class="line"></div><div class="line">  <a class="code" href="structkv_diag_sample.html">kvDiagSample</a> sample;</div><div class="line"></div><div class="line">  <span class="keywordflow">while</span> (1) {</div><div class="line">    <span class="comment">/* Wait at most 100 ms for a new sample. */</span></div><div class="line">    stat = <a class="code" href="group__kvdiag__diagnostics.html#gaab9b2bc0856a7200d1f76533092eb023">kvDiagReadSampleWait</a>(hnd, &amp;sample, 100);</div><div class="line">    <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">      printSample(&amp;sample);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">/* Check Ctrl-C. */</span></div><div class="line">    <span class="keywordflow">if</span> (interrupt) {</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">/* Stop the analyzer. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga685c95a3e168601a28e680d9c820dd8a">kvDiagStop</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagStop&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Detach the analyzer, since we&#39;re not going to restart it. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga61263295b3feaad95c257eef05a9579b">kvDiagDetachAnalyzer</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagDetachAnalyzer&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Close the CAN channel. */</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;canClose&quot;</span>, stat);</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> status)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (status != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">    printf(<span class="stringliteral">&quot;%s returned %d\n&quot;</span>, name, status);</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> intHandler(<span class="keywordtype">int</span> unused)</div><div class="line">{</div><div class="line">  interrupt = 1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> printEdges(<a class="code" href="structkv_diag_sample.html">kvDiagSample</a> *sample)</div><div class="line">{</div><div class="line">  uint64_t time = sample-&gt;<a class="code" href="structkv_diag_sample.html#a028e7a23cc3979d738320763b3e1a9d5">sample</a>.<a class="code" href="structkv_diag_sample.html#abfcbbec26161f496aee0dd71366502e6">startTime</a>;</div><div class="line">  <span class="keywordtype">int</span> value = sample-&gt;<a class="code" href="structkv_diag_sample.html#a028e7a23cc3979d738320763b3e1a9d5">sample</a>.<a class="code" href="structkv_diag_sample.html#a4df0c56a77270fc5a9aa651d214a76b8">startValue</a>;</div><div class="line">  <span class="keywordtype">int</span> i = 0;</div><div class="line">  printf(<span class="stringliteral">&quot;Start \t  End \t Length (microseconds) \t Value\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;-----------------------------------------------\n&quot;</span>);</div><div class="line">  <span class="keywordflow">do</span> {</div><div class="line">    printf(<span class="stringliteral">&quot;%6llu \t %6llu \t %2.2f \t\t  %u\n&quot;</span>,</div><div class="line">           time,</div><div class="line">           time + sample-&gt;<a class="code" href="structkv_diag_sample.html#a028e7a23cc3979d738320763b3e1a9d5">sample</a>.<a class="code" href="structkv_diag_sample.html#ab65eca6d0278db7a541d1064e317df3f">edgeTimes</a>[i],</div><div class="line">           (1.0 * sample-&gt;<a class="code" href="structkv_diag_sample.html#a028e7a23cc3979d738320763b3e1a9d5">sample</a>.<a class="code" href="structkv_diag_sample.html#ab65eca6d0278db7a541d1064e317df3f">edgeTimes</a>[i]) / (1.0 * sample-&gt;<a class="code" href="structkv_diag_sample.html#a028e7a23cc3979d738320763b3e1a9d5">sample</a>.<a class="code" href="structkv_diag_sample.html#a6755dc51cc03d549fce2d30c19665521">sampleFreq</a>),</div><div class="line">           value);</div><div class="line">    time += sample-&gt;<a class="code" href="structkv_diag_sample.html#a028e7a23cc3979d738320763b3e1a9d5">sample</a>.<a class="code" href="structkv_diag_sample.html#ab65eca6d0278db7a541d1064e317df3f">edgeTimes</a>[i];</div><div class="line">    value = !value;</div><div class="line">    i++;</div><div class="line">  } <span class="keywordflow">while</span> (i &lt; sample-&gt;sample.<a class="code" href="structkv_diag_sample.html#a50dd4913191a6339ab1369a41ba3b2e1">edgeCount</a>);</div><div class="line">  printf(<span class="stringliteral">&quot;-----------------------------------------------\n\n&quot;</span>);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> printSample(<a class="code" href="structkv_diag_sample.html">kvDiagSample</a> *sample)</div><div class="line">{</div><div class="line">  printf(<span class="stringliteral">&quot;CAN ID: 0x%x SEQ: %u. DLC: %u, flags: 0x%x, data:&quot;</span>,</div><div class="line">         sample-&gt;<a class="code" href="structkv_diag_sample.html#ac86a8d638ef0cfbbc4c9fda9942c3df6">msg</a>.<a class="code" href="structkv_diag_sample.html#a7441ef0865bcb3db9b8064dd7375c1ea">id</a>,</div><div class="line">         sample-&gt;<a class="code" href="structkv_diag_sample.html#a6cef1188df0b5529c925b53ae426610b">header</a>.<a class="code" href="structkv_diag_sample.html#ab793262012344621455faf9266acbac2">seqno</a>,</div><div class="line">         sample-&gt;<a class="code" href="structkv_diag_sample.html#ac86a8d638ef0cfbbc4c9fda9942c3df6">msg</a>.<a class="code" href="structkv_diag_sample.html#a6c4c124b5735c7cd34682cb45760c538">dlc</a>,</div><div class="line">         sample-&gt;<a class="code" href="structkv_diag_sample.html#ac86a8d638ef0cfbbc4c9fda9942c3df6">msg</a>.<a class="code" href="structkv_diag_sample.html#adf916204820072417ed73a32de1cefcf">flag</a>);</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (sample-&gt;<a class="code" href="structkv_diag_sample.html#ac86a8d638ef0cfbbc4c9fda9942c3df6">msg</a>.<a class="code" href="structkv_diag_sample.html#a6c4c124b5735c7cd34682cb45760c538">dlc</a> &gt; 0) {</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i &lt; sample-&gt;<a class="code" href="structkv_diag_sample.html#ac86a8d638ef0cfbbc4c9fda9942c3df6">msg</a>.<a class="code" href="structkv_diag_sample.html#a6c4c124b5735c7cd34682cb45760c538">dlc</a>; i++) {</div><div class="line">      printf(<span class="stringliteral">&quot; %02X&quot;</span>, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)sample-&gt;<a class="code" href="structkv_diag_sample.html#ac86a8d638ef0cfbbc4c9fda9942c3df6">msg</a>.<a class="code" href="structkv_diag_sample.html#a511368d325b346716e874b6d878dad19">data</a>[i]);</div><div class="line">    }</div><div class="line">  }</div><div class="line">  printf(<span class="stringliteral">&quot;\n\n&quot;</span>);</div><div class="line">  printEdges(sample);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> usage()</div><div class="line">{</div><div class="line">  printf(</div><div class="line">    <span class="stringliteral">&quot;Usage: &#39;normal_example -ch=n&#39;, &quot;</span></div><div class="line">    <span class="stringliteral">&quot;where &#39;n&#39; is a CANlib channel with analyzer capability.\n\n&quot;</span></div><div class="line">    <span class="stringliteral">&quot;Note: This program requires a working CAN bus with traffic to function properly.\n&quot;</span>);</div><div class="line">}</div></div><!-- fragment --><p><br />
<b>Example</b>: Continuously measure bitrate of a CAN bus using <a class="el" href="kv_diag_8h.html#a64bb36d4e56a896eb2af5dc09cce1542">DIAG_PROGRAM_TYPE_AUTOBAUD</a> .</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;stdio.h&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;signal.h&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="canlib_8h.html">canlib.h</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="kv_diag_8h.html">kvDiag.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> status); <span class="comment">/* Checks for errors */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> intHandler(<span class="keywordtype">int</span> unused); <span class="comment">/* Handle Ctrl-C */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> usage(); <span class="comment">/* Print usage */</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> interrupt = 0;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">  <span class="keywordtype">int</span> channel = -1;</div><div class="line"></div><div class="line">  <span class="comment">/* Parse command-line parameters. */</span></div><div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=1; i&lt;argc; i++) {</div><div class="line">    <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-ch=%d&quot;</span>, &amp;channel) == 1);</div><div class="line">    <span class="keywordflow">else</span> {</div><div class="line">      usage();</div><div class="line">      exit(1);</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (channel == -1) {</div><div class="line">    usage();</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line"></div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line">  <span class="comment">/* We open the channel with the EXCLUSIVE flag to not let any other</span></div><div class="line"><span class="comment">   * application interfere with the CANtegrity channel */</span></div><div class="line">  hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(channel, <a class="code" href="canlib_8h.html#afacd9e186d273d0dc24956ea0d433822">canOPEN_EXCLUSIVE</a>);</div><div class="line"></div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">  <span class="comment">/* Attach an analyzer to an open CAN channel. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga76d09ccb89cf756d954144582f730330">kvDiagAttachAnalyzer</a>(hnd, <a class="code" href="kv_diag_8h.html#a29188edd320816f6f3b4e98e6b331a66">DIAG_ANALYZER_TYPE_DEFAULT</a>);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagAttachAnalyzer&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* We want to run DIAG_PROGRAM_TYPE_AUTOBAUD. It doesn&#39;t need a configuration,</span></div><div class="line"><span class="comment">   * so we pass an empty JSON string. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#gad615ea3768542e4d48f10d0dca4ef46d">kvDiagSetProgram</a>(hnd, <a class="code" href="kv_diag_8h.html#a64bb36d4e56a896eb2af5dc09cce1542">DIAG_PROGRAM_TYPE_AUTOBAUD</a>, <span class="stringliteral">&quot;{}&quot;</span>);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagSetProgram&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Start the analyzer. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga9cb62a80b9ffe4ea80b22acfb49bd3a4">kvDiagStart</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagStart&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Continuously measure bitrate until Ctrl-C is pressed. */</span></div><div class="line">  signal(SIGINT, intHandler);</div><div class="line"></div><div class="line">  <a class="code" href="structbitrates__t.html">bitrates_t</a> btrs;</div><div class="line">  <span class="keywordflow">while</span> (1) {</div><div class="line">    Sleep(100); <span class="comment">/* Wait for samples to accumulate. */</span></div><div class="line">    stat = <a class="code" href="group__kvdiag__diagnostics.html#gabc77cf695c7fb518263d32539a3aec9e">kvDiagCalculateBitrate</a>(hnd, &amp;btrs);</div><div class="line">    <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">      printf(<span class="stringliteral">&quot;Estimated CAN bitrate to %.0f KBit/s with quality %d %%.\n&quot;</span>,</div><div class="line">             btrs.<a class="code" href="structbitrates__t.html#a9b69ac1d9a035cf32487c02eb1e90368">bitrate</a>.<a class="code" href="structbitrate__t.html#a8cbd72632a1cb167e5ca769b067cfcec">bitrate</a>, btrs.<a class="code" href="structbitrates__t.html#a9b69ac1d9a035cf32487c02eb1e90368">bitrate</a>.<a class="code" href="structbitrate__t.html#acac7df77df55f2a1bfd8d8d18340b773">quality</a>);</div><div class="line">    }</div><div class="line">    <span class="comment">/* Reset bitrate calculation to get a fresh measurement. */</span></div><div class="line">    stat = <a class="code" href="group__kvdiag__diagnostics.html#ga65bbdc1e8c8bf4affc8ba2041d72ff1a">kvDiagResetBitrateCalculation</a>(hnd);</div><div class="line">    check(<span class="stringliteral">&quot;kvDiagResetBitrateCalculation&quot;</span>, stat);</div><div class="line"></div><div class="line">    <span class="comment">/* Check Ctrl-C. */</span></div><div class="line">    <span class="keywordflow">if</span> (interrupt) {</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">/* Stop the analyzer. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga685c95a3e168601a28e680d9c820dd8a">kvDiagStop</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagStop&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Detach the analyzer, since we&#39;re not going to restart it. */</span></div><div class="line">  stat = <a class="code" href="group__kvdiag__diagnostics.html#ga61263295b3feaad95c257eef05a9579b">kvDiagDetachAnalyzer</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;kvDiagDetachAnalyzer&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">/* Close the CAN channel. */</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line">  check(<span class="stringliteral">&quot;canClose&quot;</span>, stat);</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> status)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (status != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">    printf(<span class="stringliteral">&quot;%s returned %d\n&quot;</span>, name, status);</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> intHandler(<span class="keywordtype">int</span> unused)</div><div class="line">{</div><div class="line">  interrupt = 1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> usage()</div><div class="line">{</div><div class="line">  printf(</div><div class="line">    <span class="stringliteral">&quot;Usage: &#39;autobaud_example -ch=n&#39;, &quot;</span></div><div class="line">    <span class="stringliteral">&quot;where &#39;n&#39; is a CANlib channel with analyzer capability.\n\n&quot;</span></div><div class="line">    <span class="stringliteral">&quot;Note: This program requires a working CAN bus with traffic to function properly.\n&quot;</span>);</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_canlib.html">CAN bus API (CANlib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
